﻿namespace QMVnSoft.Controls.ChildGridControls
{
    using QMVnSoft;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class OptionColumn : DataGridViewColumn
    {
        public OptionColumn()
        {
            OptionCell cell = new OptionCell();
            this.CellTemplate = cell;
            this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            base.SortMode = DataGridViewColumnSortMode.Automatic;
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                OptionCell cell = value as OptionCell;
                if ((value != null) && (cell == null))
                {
                    throw new InvalidCastException("Value provided for CellTemplate must be of type DataGridViewTextOptionElements.DataGridViewTextOptionCell or derive from it.");
                }
                base.CellTemplate = value;
            }
        }

        [Category("Option column properties"), RefreshProperties(RefreshProperties.All), DefaultValue(0), Description("Get or set disabled forecolor.")]
        public Color DisabledForeColor
        {
            get
            {
                if (this.OptionCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.OptionCellTemplate.DisabledForeColor;
            }
            set
            {
                this.OptionCellTemplate.DisabledForeColor = value;
            }
        }

        [RefreshProperties(RefreshProperties.All), Description("Get or set inputmask."), DefaultValue(""), Category("Option column properties")]
        public string InputMask
        {
            get
            {
                if (this.OptionCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.OptionCellTemplate.InputMask;
            }
            set
            {
                if (this.OptionCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.OptionCellTemplate.InputMask = value;
                if (((base.DataGridView != null) && (base.DataGridView.CurrentCell != null)) && (base.DataGridView.CurrentCell is OptionCell))
                {
                    (base.DataGridView.CurrentCell as OptionCell).SetInputMask(base.DataGridView.CurrentCell.RowIndex, value);
                }
            }
        }

        private OptionCell OptionCellTemplate
        {
            get
            {
                return (OptionCell) base.CellTemplate;
            }
            set
            {
                if ((value != null) && !value.GetType().IsAssignableFrom(typeof(OptionCell)))
                {
                    throw new InvalidCastException("Must be a CustomDataGridViewOptionTextBoxCell");
                }
                base.CellTemplate = value;
            }
        }

        [Description("Get or set max text length."), DefaultValue(3), RefreshProperties(RefreshProperties.All), Category("Option column properties")]
        public AppInstance.TextControlLength UserTextLength
        {
            get
            {
                if (this.OptionCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.OptionCellTemplate.UserTextLength;
            }
            set
            {
                this.OptionCellTemplate.UserTextLength = value;
            }
        }

        [DefaultValue(2), RefreshProperties(RefreshProperties.All), Category("Option column properties"), Description("Get or set trim mode.")]
        public AppInstance.TextControlTrimMode UserTrimMode
        {
            get
            {
                if (this.OptionCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.OptionCellTemplate.UserTrimMode;
            }
            set
            {
                this.OptionCellTemplate.UserTrimMode = value;
            }
        }
    }
}

